package com.itany.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.DictConstant;
import com.itany.cms.constant.StatusConstant;
import com.itany.cms.dao.ResourceMapper;
import com.itany.cms.dao.UserMapper;
import com.itany.cms.entity.Comment;
import com.itany.cms.entity.Resource;
import com.itany.cms.entity.Resource;
import com.itany.cms.entity.User;
import com.itany.cms.exception.RequestParameterErrorException;
import com.itany.cms.service.ResourceService;
import com.itany.cms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ResourceServiceImpl implements ResourceService {
    @Autowired
    private ResourceMapper resourceMapper;
    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Resource> findByParams(Resource resourceParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.RESOURCE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.RESOURCE_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<User> wrapper2 = new QueryWrapper<>();
        if (!ParameterUtil.isNull(resourceParam.getUserName())) {
            wrapper2.like("nickname", resourceParam.getUserName());
        }
        List<User> users = userMapper.selectList(wrapper2);
        QueryWrapper<Resource> wrapper = new QueryWrapper<>();
        if (!ParameterUtil.isNull(resourceParam.getTitle())) {
            wrapper.like("title", resourceParam.getTitle());
        }
        if (!ParameterUtil.isNull(resourceParam.getUserName())) {
            for (User user : users) {
                wrapper.eq("user_id", user.getId());
            }
        }
        if (!ParameterUtil.isNull(resourceParam.getStartDate()) || !ParameterUtil.isNull(resourceParam.getEndDate())) {
            wrapper.between("create_Date", resourceParam.getStartDate(), resourceParam.getEndDate());
        }
        if (!ParameterUtil.isNull(resourceParam.getStatus())) {
            wrapper.eq("status", resourceParam.getStatus());
        }
        if (!ParameterUtil.isNull(resourceParam.getFileType())) {
            wrapper.eq("file_type", resourceParam.getFileType());
        }
        List<Resource> resources = resourceMapper.selectList(wrapper);
        for (Resource resource:resources) {
            QueryWrapper<User> wrapper1 = new QueryWrapper();
            wrapper1.eq("id",resource.getUserId());
            List<User> users1 = userMapper.selectList(wrapper1);
            resource.setUserName(users1.get(0).getNickname());
        }
        PageInfo<Resource> resourcePage = new PageInfo<>(resources);
        return resourcePage;
    }

    @Override
    public PageInfo<Resource> findDetail(Resource resourceParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.RESOURCE_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.RESOURCE_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        QueryWrapper<Resource> wrapper =new QueryWrapper<>();
        wrapper.eq("user_id",resourceParam.getUserId());
        List<Resource> resources = resourceMapper.selectList(wrapper);
        PageInfo<Resource> resourcePageInfo = new PageInfo<>(resources);
        return resourcePageInfo;
    }

    @Override
    public void modifyStatus(String id, String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        Resource resource = new Resource();
        resource.setId(Integer.parseInt(id));
        resource.setStatus(Integer.parseInt(status));
        if (resource.getStatus() == StatusConstant.COURSE_TYPE_STATUS_ENABLED) {
            resource.setStatus(StatusConstant.COURSE_TYPE_STATUS_DISABLED);
        } else {
            resource.setStatus(StatusConstant.COURSE_TYPE_STATUS_ENABLED);
        }
        resourceMapper.updateById(resource);
    }
}
